package com.example.yskjproject.ui.fragment;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.yskjproject.R;
import com.example.yskjproject.adapter.MessageAdapter;
import com.example.yskjproject.base.BaseFragment;
import com.example.yskjproject.content.Content;
import com.example.yskjproject.databinding.MessageFragmentBinding;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.MessageDateModel;
import com.example.yskjproject.model.MessageListModel;
import com.google.common.collect.Lists;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * MessageFragment  消息
 * <p>
 * 作者：zx
 * 创建时间：2024/4/12
 * 消息处理
 *
 */
public class MessageFragment extends BaseFragment {

    private MessageFragmentBinding messageDataBind;
    @Override
    protected MessageFragmentBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        messageDataBind = MessageFragmentBinding.inflate(inflater,parent,false);
        return messageDataBind;
    }

    private List<MessageDateModel> messgaeListDate = Lists.newArrayList();


    @Override
    protected void onData() {
        getMessageListDate();
        messgaeListDate.clear();
        messageDataBind.title.titleTv.setText(getContext().getString(R.string.message_list));
        messageDataBind.messageRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        messageDataBind.swipeRefreshLayout.setOnRefreshListener(() -> getMessageListDate());
    }

    /**
     * 回去message
     */
    private void getMessageListDate() {
        OkGo.<LzyResponse<MessageListModel>>get(Urls.MESSAGE_LIST + "?page=" + Content.page + "&limit=" + Content.limit).execute(new JsonCallback<LzyResponse<MessageListModel>>() {
            @Override
            public void onSuccess(Response<LzyResponse<MessageListModel>> response) {
                MessageListModel data = response.body().data;
                if (data.getMessage_list().isEmpty()) {
                    ToastUtil.toastShortMessage("暂无消息");
                    return;
                }
                messgaeListDate.addAll(data.getMessage_list());
                messageDataBind.swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(Response<LzyResponse<MessageListModel>> response) {
                super.onError(response);
                messageDataBind.swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

}
